<?php

namespace App\Admin\Controllers;

use App\Admin\Models\CustomerModel;
use App\Http\Controllers\Controller;
use App\Models\ApplicationModel;
use Encore\Admin\Controllers\ModelForm;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Illuminate\Http\Request;
use Encore\Admin\Widgets\InfoBox;
use Encore\Admin\Widgets\Box;
use Encore\Admin\Widgets\Table;

class ApplicationController extends Controller
{
    use ModelForm;
    protected $_method=0;


    public function index(Request $request)
    {
        $this->_method = $request->route('method');
        return Admin::content(function (Content $content) {
            
          switch($this->_method){
              case '1':
                  $content->header('申请');
                  break;
              case '2':
                  $content->header('待办');
                  break;
              case '3':
                  $content->header('已办');
                  break;
              default:
                  $content->header('我的任务');
                  break;
              
          }  
            $content->description('');
//            $method = isset($_GET['method'])?$_GET['method']:0;
            $content->body($this->mgrid());
        });
    }
    
    
     /**
     * Edit interface.
     *
     * @param $id
     * @return Content
     */
    public function edit(Request $request,$id)
    {
        return Admin::content(function (Content $content) use ($id,$request) {

            $content->header('申请编辑');
            $content->description('');

            $content->row($this->form()->edit($id));

            $point = $request->point;

            $oGeo = new \App\Admin\Models\GeoModel;
            $this->aList = $oGeo->getPoints($id,$point);
//            var_dump($this->aList->toArray());exit;
            if(!empty($this->aList->toArray())){  
//                    var_dump($this->aList);exit;
                $headers = ['Id', '分类信息', '操作'];
                $rows = array();

           //            $content->row(function ($row) {
                foreach($this->aList as $aOne){
//                    var_dump($aOne);exit;
                    $delete = '<a href="javascript:;" data-id="'.$aOne->id.'" class="grid-row-delete">删除</a>';
                    $edit = '<a href="'.route("geo.edit",['id'=>$aOne->id]).'">修改</a>';
                    $rows[]=[$aOne->id, '<a href="/admin/geo/'.$aOne->id.'/edit">'.$aOne->title.'</a>',$edit,$delete];
//                $row->column(3, new InfoBox($aOne->title, $aOne->title, 'aqua', '/admin/geo/'.$aOne->id.'/edit', ''));
                }
//            });
                $html = '
                <div class="pull-right">
                    <div class="form-inline pull-right">
                        <form action="'.route("application.edit",$id).'" method="get" pjax-container="">
                            <fieldset>
                                <div class="input-group input-group-sm">
                                    <span class="input-group-addon"><strong>分类信息</strong></span>
                                    <input type="text" class="form-control" placeholder="分类信息" name="point" id="pointname" value="">
                                </div>
                                <div class="btn-group btn-group-sm">
                                    <button type="submit" class="btn btn-primary"><i class="fa fa-search"></i></button>
                                </div>
                    
                            </fieldset>
                        </form>
                    </div>
                
                    
                    <div class="btn-group pull-right" style="margin-right: 10px">
                        <a href="'.route("geo.create").'" class="btn btn-sm btn-success">
                            <i class="fa fa-save"></i>&nbsp;&nbsp;新增
                        </a>
                    </div>
        
                </div>
                
                ';
                $tools = [$html];
                 $content->row((new Box('项目信息', new Table($headers, $rows), $tools))->style('info')->solid());

            }
            $js = "
            <style>
            .pagination{
                float:right
            }
            </style>
            <script>  
            $('.grid-row-delete').unbind('click').click(function() {
                if(confirm(\"确认删除?\")) {
                    $.ajax({
                        method: 'post',
                        url: '/admin/geo/' + $(this).data('id'),
                        data: {
                            _method:'delete',
                            _token:LA.token,
                        },
                        success: function (data) {
                            $.pjax.reload('#pjax-container');
            
                            if (typeof data === 'object') {
                                if (data.status) {
                                    toastr.success(data.message);
                                } else {
                                    toastr.error(data.message);
                                }
                            }
                        }
                    });
                }
            });

            </script>";
            $content->body($js);
            $content->body($this->aList->links());
        });
    }
    
    public function save(){
//        var_dump($_POST);exit;
        $this->form()->store();
    }

    /**
     * Create interface.
     *
     * @return Content
     */
    public function create()
    {
        return Admin::content(function (Content $content) {

            $content->header('新增申请');
            $content->description('');

            $content->body($this->form());
        });
    }


    
    public function mgrid()
    {
//        $this->_method=$method;
        return Admin::grid(ApplicationModel::class, function (Grid $grid) {

             $grid->renderCreateButton('新申请');  
             $grid->resource('admin/application');
//            $grid->id('ID')->sortable();
            $grid->column('place','所在地')->sortable();
            $grid->column('projectname','项目名称');
            $grid->column('assignerid','项目经理')->value(function($userId){
                $oManager = new ApplicationModel();
//                var_dump($oManager->getManager($userId));
                $oM = $oManager->getManager($userId);
//                var_dump($oM);
                return $oM->name;
            });
            $grid->column('mode','当前任务')->value(function($modeId){
                switch ($modeId){
                    case '1':
                        return '勘探';
                        break;
                    case '2':
                        return '取样';
                        break;
                    case '3':
                        return '分析';
                        break;
                    case '4':
                        return '修复';
                        break;
                }
            });
            $grid->column('assigndate','分配时间');

//            $grid->created_at();
//            $grid->updated_at();
            
            $grid->filter(function ($filter) {
    
                  // 设置created_at字段的范围查询
                     $filter->like('projectname', '项目名称');
                     $filter->between('startdate','开始时间')->datetime();
                     $filter->disableIdFilter();
             });
//             var_dump($this->_method);
//             $method = $this->_method; 
//             var_dump($grid->model()->table);exit;
             if($this->_method>0){
                 $grid->model()->where('stage','=',$this->_method); 
             }
//             $aMe = Admin::user();
//             var_dump($grid->resouce());exit;
             $grid->disableRowSelector();
            $grid->disableExport();
            $grid->disableBatchDeletion();
                    
//            $grid->disableActions();
        });
    }
    
    //申请表单
     protected function form()
    {
        
        return Admin::form(ApplicationModel::class, function (Form $form) {
             $id = Admin::user()->id;
             $oP = new ApplicationModel();
             $oC = new CustomerModel();
             $aPs = $oP->getManagerNames($id);
             $aCs = $oC->getCustomerList();
//              var_dump("zgyrdf");exit;
//                var_dump($aPs);
            $form->text('place','所在地')->rules('required');     
            $form->select('company','公司名称')->options($aCs)->rules('required');          
            $form->text('projectname','项目名称')->rules('required');
            $form->select('mode','处理任务')->options(array('1'=>'勘探','2'=>'取样','3'=>'分析','4'=>'修复'));
            $form->text('address','地址')->rules('required');
            $form->date('startdate','开始时间')->rules('required');
            $form->date('enddate','结束时间');
            $form->select('stage','处理阶段')->options(array('1'=>'申请','2'=>'待办','3'=>'完成'));
            $form->select('assignerid', '项目经理')->options($aPs);
            $form->text('latitude','纬度');
            $form->text('longitude','经度');
            $form->map('latitude','longitude','位置');
//            $form->setAction('/admin/projects/save');
            $form->hidden('assigneeid')->value($id);
//            $form->getPoints()->value(function($id){
//                $oGeo = new \App\Admin\Models\GeoModel;
//                $aList = $oGeo->getPoints($id);
//                return $aList;
//            });
            
//            $form->display('id', 'ID');
            $form->model()->timestamps = false;
            

//            $form->display('created_at', 'Created At');
//            $form->display('updated_at', 'Updated At');
            
//            $form->saving(function (Form $form) {
//                if ($form->projectname && $form->model()->projectname == $form->projectname) {
//                    return false;
//                }
//            });

        });
    }
}
